// pages/personal/myorder/myorder.js
var utils = require('../../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curHdIndex: 0,
        orderState: 10000,
        currentPage: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            curHdIndex: options.stateindex,
            orderState: options.stateid
        })
    },
    onShow: function() {
        var that = this;
        that.setData({
            orderList: [],
            currentPage: 1
        })
        that.onOrderList(that.data.orderState)
    },
    onOrderList: function(orderState) {
        var that = this;
        utils.showLoading();
        utils.http("api/v1/order/my_order_list", {
            order_state: orderState,
            page: that.data.currentPage
        }, function(res) {
            utils.hideLoading();
            var allpost = that.data.orderList;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas) {
                res.datas[i].number = 0;
                allpost.push(res.datas[i])
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                orderList: allpost,
                ready: res.datas.length == 0 ? 1 : 2,
            })
        })

    },
   onBohuiTap:function(e){
     wx.navigateTo({
       url: '../../reject/reject?complaintreason=' + e.currentTarget.dataset.complaintreason
     })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.onOrderList(that.data.orderState, page)
        }
    },
    //tab切换
    tab: function(e) {
        var stateId = e.currentTarget.dataset.stateid;
        var dataId = e.currentTarget.id;
        var curHdIndex = dataId;
        this.setData({
            curHdIndex: curHdIndex,
            orderState: stateId,
            currentPage: 1,
            orderList: []
        })
        this.onOrderList(stateId)
    },
    //订单详情
    onOrderDetailsTap: function(e) {
        wx.navigateTo({
            url: '../myorderdetails/myorderdetails?id=' + e.currentTarget.id,
        })
    },
    //订单评价
    onEvaluationTap: function(e) {
        wx.navigateTo({
            url: '../../evaluation/evaluation?id=' + e.currentTarget.dataset.id,
        })
    },
    //申请售后
    onAftersaleTap: function(e) {
        wx.navigateTo({
            url: '../aftersale/aftersale?id=' + e.currentTarget.dataset.id,
        })
    },
    //跳入物流进度
    onLogisticsProgressTap: function(e) {
        wx.navigateTo({
            url: '../logisticsprogress/logisticsprogress?orderid=' + e.currentTarget.dataset.orderid
        })
    },
    //投诉果店
    onComplaintTap:function(e){
        console.log(e.currentTarget.dataset.complaintstate)
        if (e.currentTarget.dataset.complaintstate==1){
            wx.showToast({
                title: '您已经投诉过了哦~',
                icon:"none"
            })
        }else{
            wx.navigateTo({
                url: '../complaint/complaint?id=' + e.currentTarget.dataset.id,
            })
        }
        
    },

    //确认收货
    onReceivingTap: function(e) {
        var that = this;
        wx.showModal({
            title: '确认收货',
            content: '您是确认收货吗',
            success(res) {
                if (res.confirm) {
                    utils.http("/api/v1/order/ok_order", {
                        order_id: e.target.dataset.id
                    }, function(res) {
                        if (res.state == 1) {
                            that.setData({
                                currentPage: 1,
                                orderList: []
                            })
                            that.onOrderList(that.data.orderState)
                        } else {
                            utils.message(res.datas)
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    //申请退货
    onReturnTap:function(e){
        console.log(e)
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/order/order_refund", {
            id: e.currentTarget.dataset.orderid
        }, function (res) {
            utils.hideLoading();
            if (res.state == 1) {
                wx.showToast({
                    title: "提交成功",
                    icon: "none"
                })
                that.setData({
                    currentPage: 1,
                    orderList: []
                })
                that.onOrderList(that.data.orderState)

            } else {
                utils.message(res.datas)
            }
        })
    },
    //提醒发货
    deliveryTap: function(e) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/order/ques_delivery", {
            order_id: e.currentTarget.dataset.id
        }, function(res) {
            utils.hideLoading();
            if (res.state == 1) {
                wx.showToast({
                    title: "已提醒成功",
                    icon: "none"
                })
            } else {
                utils.message(res.datas)
            }
        })
    },
    //取消订单
    cancelOrderTap: function(e) {
        var that = this;
      utils.http("/api/v1/order/cancel_order", {
            order_id: e.currentTarget.dataset.id
        }, function(res) {
            if (res.state == 1) {
                wx.showToast({
                    title: "您已取消订单",
                    icon: "none",
                    page: 1,
                    orderList: []

                })
                // that.onOrderList(that.data.orderState)
            } else {
                utils.message(res.datas)
            }
        })
    },
    //去付款
    onOrderTap:function(e){
        var orderid= e.currentTarget.dataset.id;
        console.log(orderid)
        var peymentcode = e.currentTarget.dataset.peymentcode;
        if (peymentcode =="wx_pay"){
            utils.showLoading();
            utils.http("/api/v1/order/again_pay_order", {
                order_id: orderid,
            }, function (res) {
                console.log(res);
                utils.hideLoading();
                if (res.state == 1) {
                    wx.requestPayment({
                        timeStamp: res.datas.timeStamp,
                        nonceStr: res.datas.nonceStr,
                        package: res.datas.package,
                        signType: res.datas.signType,
                        paySign: res.datas.paySign,
                        success(res) {
                            wx.redirectTo({
                                url: '/pages/ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
                            })
                        },
                        fail(res) {
                        }
                    })
                } else {
                    utils.message(res.datas);
                }
            })

        } else if (peymentcode == "balance") {
            wx.showModal({
                title: '钱包支付',
                content: '您确定要钱包支付吗？',
                success(res) {
                    if (res.confirm) {
                        utils.http("/api/v1/order/again_pay_order", {
                            order_id: orderid
                        }, function (res) {
                            utils.hideLoading()
                            if (res.state == 1) {
                                wx.redirectTo({
                                    url: '/pages/ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
                                })
                            } else {
                                utils.message(res.datas);
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }

        
    }

})